/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.synyx.hades.domain.AbstractPersistable;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"configPair_id", "configRevision_id"})})
@NamedQueries({
	@NamedQuery(name = "ConfigValue.findByAll", query = "from ConfigValue o")})
public class ConfigValue extends AbstractPersistable<Long> {
	private String data;

	@ManyToOne
	private ConfigPair configPair;
	
	@ManyToOne
	private ConfigRevision configRevision;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public void setConfigPair(ConfigPair configPair) {
		this.configPair = configPair;
	}

	public ConfigPair getConfigPair() {
		return configPair;
	}

	public void setConfigRevision(ConfigRevision configRevision) {
		this.configRevision = configRevision;
	}

	public ConfigRevision getConfigRevision() {
		return configRevision;
	}

	@Override
	public String toString() {
		return String.format(
				"ConfigValue [configPair=%s, configRevision=%s, data=%s]",
				configPair, configRevision, data);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((configPair == null) ? 0 : configPair.hashCode());
		result = prime * result
				+ ((configRevision == null) ? 0 : configRevision.hashCode());
		result = prime * result + ((data == null) ? 0 : data.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof ConfigValue)) {
			return false;
		}
		ConfigValue other = (ConfigValue) obj;
		if (configPair == null) {
			if (other.configPair != null) {
				return false;
			}
		} else if (!configPair.equals(other.configPair)) {
			return false;
		}
		if (configRevision == null) {
			if (other.configRevision != null) {
				return false;
			}
		} else if (!configRevision.equals(other.configRevision)) {
			return false;
		}
		if (data == null) {
			if (other.data != null) {
				return false;
			}
		} else if (!data.equals(other.data)) {
			return false;
		}
		return true;
	}
}
